package com.cpfmat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cpfmat.entity.OtherUser;
import com.cpfmat.mapper.OtherUserMapper;
import com.cpfmat.service.IOtherUserService;

@Service
public class OtherUserServiceImpl extends ServiceImpl<OtherUserMapper, OtherUser> implements IOtherUserService {

	@Autowired
	private OtherUserMapper otherUserMapper;

	@Override
	public int customUpdate(OtherUser user) {
		// TODO Auto-generated method stub
		return otherUserMapper.customUpdate(user);
	}

	@Override
	public OtherUser checkLogin(String loginName, String loginPwd) {
		if(StringUtils.isNotBlank(loginName) && StringUtils.isNotBlank(loginPwd)) {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("userName", loginName);
			param.put("password", loginPwd);
			List<OtherUser> loginUser = otherUserMapper.getLoginUser(param);
			if(loginUser != null && loginUser.size() == 1){
				return loginUser.get(0);
			}
		}
		return null;
	}

	@Override
	public OtherUser hasUserByloginName(String loginName) {
		// TODO Auto-generated method stub
		OtherUser user = new OtherUser();
		user.setLoginName(loginName);
		user.setDelFlag(0);
		return otherUserMapper.selectOne(user);
	}

	@Override
	public OtherUser hasUserByPhonenum(String phonenum) {
		// TODO Auto-generated method stub
		OtherUser user = new OtherUser();
		user.setPhonenum(phonenum);
		user.setDelFlag(0);
		return otherUserMapper.selectOne(user);
	}

	@Override
	public OtherUser hasUserByEmail(String email) {
		// TODO Auto-generated method stub
		OtherUser user = new OtherUser();
		user.setEmail(email);
		user.setDelFlag(0);
		return otherUserMapper.selectOne(user);
	}

	@Override
	public OtherUser checkUserById(String sqlId) {
		// TODO Auto-generated method stub
		OtherUser user = new OtherUser();
		user.setSqlId(sqlId);
		user.setDelFlag(0);
		return otherUserMapper.selectOne(user);
	}

	@Override
	public Page<OtherUser> getcontentByUser(String userName, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> param = new HashMap<String, Object>();
		if (pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);

		if (StringUtils.isNotBlank(userName)) {
			param.put("userName", userName);
		}

		long counts = otherUserMapper.countAllList(param);

		List<OtherUser> resultList = null;
		if (counts > 0) {
			resultList = otherUserMapper.getAllList(param);
		}
		Page<OtherUser> resultPage = new Page<OtherUser>();
		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);
		return resultPage;
	}

	@Override
	public Page<OtherUser> getAllPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> param = new HashMap<String, Object>();
		if (pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);


		long counts = otherUserMapper.countAllList(param);

		List<OtherUser> resultList = null;
		if (counts > 0) {
			resultList = otherUserMapper.getAllList(param);
		}
		Page<OtherUser> resultPage = new Page<OtherUser>();
		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);
		return resultPage;
	}

}
